import httpUtils from '@/utils/httpUtils.js'
import ui from '@/utils/ui.js'

// 搜索框内容改变的回调
function keywordChange(_this) {
  if (_this.keyword === "") {
    return
  }
  clearTimeout(_this.timerout)
  _this.timerout = setTimeout(() => {
    httpUtils.request({
      url: `/home/search?keyword=${_this.keyword}`,
      method: 'get',
    }).then(data => {
      console.log(data);
      _this.searchList = data.data
    }).catch(e => {
      console.log(e);
    })
  }, 200)
}
// 搜索控件点击事件回调
function search(_this) {
  _this.searchClass(_this.keyword)
  _this.keywordInit(_this.keyword)
}
// 清除历史记录功能
function clearKeyword(_this) {
  _this.history = []
}
// 点击历史记录记录功能
function historyItem({_this,data}) {
  console.log(data);
  _this.searchClass(data.keyword)
}
// 联想关键词点击事件函数
function keywordClick({_this,data}) {
  _this.keywordInit(data.class_name)
}
// 搜索课程的函数
function searchClass({_this,className}) {
  httpUtils.request({
    url: `/home/searchClass?className=${className}`,
    method: 'get'
  }).then(data => {
    _this.classList = data.data
    console.log(data);
  }).catch(e => {
    _this.classList = []
    console.log(e);
  })
}
// 初始化函数
function keywordInit({_this,key}) {
  console.log(key);
  if (_this.history.indexOf(key) === -1) {
    _this.history.unshift(key)
  }
  _this.searchClass(key)
  _this.keyword = ''
}

module.exports = {
  keywordChange,
  search,
  clearKeyword,
  historyItem,
  keywordClick,
  searchClass,
  keywordInit,
}
